<?php

require_once('Adept/Binding/IValue.php');
require_once('Adept/Binding/IMethod.php');

class Adept_Binding_Expression implements Adept_Binding_IMethod, Adept_Binding_IValue 
{
    
    protected $expression;
    
    public function __construct($expression) 
    {
        $expression = preg_replace("/\.([\w]+)/", "['\\1']", $expression);
        $expression = preg_replace('/\$(\w+)/', '$context[\'\\1\']', $expression);        
        $this->expression = $expression;
    }
    
    public function getValue() 
    {
        $result = null;
        $context = Adept_Context::getInstance();
        
        eval('$result = (' . $this->expression . '); ');
        return $result;
    }
    
    public function setValue($value) 
    {
       throw new Adept_Exception("Cannot set Epxression Binding Property ");
    }
    
    public function invoke($args) 
    {
       throw new Adept_Exception("Cannot invoke Epxression Binding Property ");
    }
    
    public function getExpression() 
    {
       return $this->expression;
    }
    
    public function setExpression($expression) 
    
    {
       $this->expression = $expression;
    }
    
}
